<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="道路名称" prop="roadName">
        <el-input
          v-model="queryParams.roadName"
          placeholder="请输入道路名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="公路类型" prop="roadManagementCategory">
        <el-input
          v-model="queryParams.roadManagementCategory"
          placeholder="请输入公路类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="道路名称" align="center" prop="roadName" />
      <el-table-column label="编号" align="center" prop="roadId" />
      <el-table-column label="公路方向" align="center" prop="roadDirection" />
      <el-table-column label="公路起点" align="center" prop="roadStartAt" />
      <el-table-column label="公路终点" align="center" prop="roadEndAt" />
      <el-table-column label="设计单位" align="center" prop="roadDesignUnit" />
      <el-table-column label="施工单位" align="center" prop="roadConstructionUnit" />
      <el-table-column label="管理单位" align="center" prop="roadManagementUnit" />
      <el-table-column label="公路等级" align="center" prop="roadLevel" />
      <el-table-column label="设计时速" align="center" prop="roadDesignSpeed" />
      <el-table-column label="公路长度" align="center" prop="roadLength" />
      <el-table-column label="年日均交通量" align="center" prop="roadTrafficVolumeLevel" />
      <el-table-column label="公路类型" align="center" prop="roadManagementCategory" />
      <el-table-column label="标线" align="center" prop="roadinfoRemarks" />
      <el-table-column label="养护单位" align="center" prop="roadMaintenanceUnit" />
      <el-table-column label="竣工时间" align="center" prop="roadBuildDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.roadBuildDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="制表单位" align="center" prop="roadTabulationUnit" />
      <el-table-column label="制表日期" align="center" prop="roadTabulationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.roadTabulationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公路状态" align="center" prop="roadStatus" />
      <el-table-column label="备注" align="center" prop="roadCode" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

   <!--添加道路信息对话框-->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="道路名称" prop="roadName">
          <el-input v-model="form.roadName" placeholder="请输入道路名称" />
        </el-form-item>
        <el-form-item label="公路方向" prop="roadDirection">
          <el-input v-model="form.roadDirection" placeholder="请输入公路方向" />
        </el-form-item>
        <el-form-item label="公路起点" prop="roadStartAt">
          <el-input v-model="form.roadStartAt" placeholder="请输入公路起点" />
        </el-form-item>
        <el-form-item label="公路终点" prop="roadEndAt">
          <el-input v-model="form.roadEndAt" placeholder="请输入公路终点" />
        </el-form-item>
        <el-form-item label="设计单位" prop="roadDesignUnit">
          <el-input v-model="form.roadDesignUnit" placeholder="请输入设计单位" />
        </el-form-item>
        <el-form-item label="施工单位" prop="roadConstructionUnit">
          <el-input v-model="form.roadConstructionUnit" placeholder="请输入施工单位" />
        </el-form-item>
        <el-form-item label="管理单位" prop="roadManagementUnit">
          <el-input v-model="form.roadManagementUnit" placeholder="请输入管理单位" />
        </el-form-item>
        <el-form-item label="公路等级" prop="roadLevel">
          <el-input v-model="form.roadLevel" placeholder="请输入公路等级" />
        </el-form-item>
        <el-form-item label="设计时速" prop="roadDesignSpeed">
          <el-input v-model="form.roadDesignSpeed" placeholder="请输入设计时速" />
        </el-form-item>
        <el-form-item label="公路长度" prop="roadLength">
          <el-input v-model="form.roadLength" placeholder="请输入公路长度" />
        </el-form-item>
        <el-form-item label="年日均交通量" prop="roadTrafficVolumeLevel">
          <el-input v-model="form.roadTrafficVolumeLevel" placeholder="请输入年日均交通量" />
        </el-form-item>
        <el-form-item label="公路类型" prop="roadManagementCategory">
          <el-input v-model="form.roadManagementCategory" placeholder="请输入公路类型" />
        </el-form-item>
        <el-form-item label="标线" prop="roadinfoRemarks">
          <el-input v-model="form.roadinfoRemarks" placeholder="请输入标线" />
        </el-form-item>
        <el-form-item label="养护单位" prop="roadMaintenanceUnit">
          <el-input v-model="form.roadMaintenanceUnit" placeholder="请输入养护单位" />
        </el-form-item>
        <el-form-item label="竣工时间" prop="roadBuildDate">
          <el-date-picker clearable
            v-model="form.roadBuildDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择竣工时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="制表单位" prop="roadTabulationUnit">
          <el-input v-model="form.roadTabulationUnit" placeholder="请输入制表单位" />
        </el-form-item>
        <el-form-item label="制表日期" prop="roadTabulationDate">
          <el-date-picker clearable
            v-model="form.roadTabulationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择制表日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="roadCode">
          <el-input v-model="form.roadCode" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/roadmanage/info";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roadName: null,
        roadDirection: null,
        roadStartAt: null,
        roadEndAt: null,
        roadDesignUnit: null,
        roadConstructionUnit: null,
        roadManagementUnit: null,
        roadLevel: null,
        roadDesignSpeed: null,
        roadLength: null,
        roadTrafficVolumeLevel: null,
        roadManagementCategory: null,
        roadinfoRemarks: null,
        roadMaintenanceUnit: null,
        roadBuildDate: null,
        roadTabulationUnit: null,
        roadTabulationDate: null,
        roadStatus: null,
        roadCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        roadName: [
          { required: true, message: "道路名称不能为空", trigger: "blur" }
        ],
        roadDirection: [
          { required: true, message: "公路方向不能为空", trigger: "blur" }
        ],
        roadStartAt: [
          { required: true, message: "公路起点不能为空", trigger: "blur" }
        ],
        roadEndAt: [
          { required: true, message: "公路终点不能为空", trigger: "blur" }
        ],
        roadDesignUnit: [
          { required: true, message: "设计单位不能为空", trigger: "blur" }
        ],
        roadConstructionUnit: [
          { required: true, message: "施工单位不能为空", trigger: "blur" }
        ],
        roadLevel: [
          { required: true, message: "公路等级不能为空", trigger: "blur" }
        ],
        roadStatus: [
          { required: true, message: "公路状态不能为空", trigger: "blur" }
        ],
        roadCode: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        roadName: null,
        roadId: null,
        roadDirection: null,
        roadStartAt: null,
        roadEndAt: null,
        roadDesignUnit: null,
        roadConstructionUnit: null,
        roadManagementUnit: null,
        roadLevel: null,
        roadDesignSpeed: null,
        roadLength: null,
        roadTrafficVolumeLevel: null,
        roadManagementCategory: null,
        roadinfoRemarks: null,
        roadMaintenanceUnit: null,
        roadBuildDate: null,
        roadTabulationUnit: null,
        roadTabulationDate: null,
        roadStatus: 0,
        roadCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roadId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公路信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const roadId = row.roadId || this.ids
      getInfo(roadId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公路信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.roadId != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roadIds = row.roadId || this.ids;
      this.$modal.confirm('是否确认删除道路信息编号为"' + roadIds + '"的数据项？').then(function() {
        return delInfo(roadIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
